package com.zy.is.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.is.domain.SysFileInfo;
import com.zy.is.mapper.SysFileInfoMapper;
import com.zy.is.service.SysFileInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文件保存 服务层实现
 *
 * @author Yanzy
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysFileInfoServiceImpl implements SysFileInfoService {

    /**
     * 使用构造器的方法注入bean
     */
    private final SysFileInfoMapper sysFileInfoMapper;

    /**
     * 主键查询
     *
     * @param id
     * @return
     */
    @Override
    public SysFileInfo findById(Long id) {
        return sysFileInfoMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SysFileInfo sysFileInfo) {
        log.debug("sysFileInfo ===> {}", sysFileInfo.toString());
        return sysFileInfoMapper.insert(sysFileInfo);
    }

    @Override
    public int insertList(List<SysFileInfo> list) {

        int count = 0;
        for (SysFileInfo sysFileInfo : list) {
            insert(sysFileInfo);
            count ++;
        }
        return count;
    }

    @Override
    public List<SysFileInfo> findAll(SysFileInfo sysFileInfo) {
        QueryWrapper<SysFileInfo> queryWrapper = new QueryWrapper<>();

        return sysFileInfoMapper.selectList(queryWrapper);
    }

    @Override
    public Page<SysFileInfo> findAllByPage(Page<SysFileInfo> page, SysFileInfo sysFileInfo) {
        LambdaQueryWrapper<SysFileInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (null != sysFileInfo) {

            lambdaQueryWrapper.like(SysFileInfo::getFileName, sysFileInfo.getFileName());
        }
        lambdaQueryWrapper.orderByDesc(SysFileInfo::getCreateTime);
        return sysFileInfoMapper.selectPage(page, lambdaQueryWrapper);
    }

    /**
     * 主键删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public int deleteById(Long id) {
        return sysFileInfoMapper.deleteById(id);
    }
}
